
texture t0 : TEXTURE0;
sampler s0 = sampler_state{
	Texture = <t0>;
	MagFilter = Point;
	MinFilter = Point;
};

matrix g_mWorld : MATWORLD;
matrix g_mWorldViewProj:MATWORLDVIEWPROJECTION;
matrix g_mLightWorldViewProj:MATLIGHTWORLDVIEWPROJECTION;


struct LIGHTPROPERTY{
	float4 Diffuse;
	float4 Ambient;
	float4 Specular;
	float3 vPosition;
	float3 vDirection;
	float Range;
	float Falloff;
	float Attenuation0;
	float Attenuation1;
	float Attenuation2;
	float Theta;
	float Phi;
};

LIGHTPROPERTY g_Light : LIGHTPROPERTY;

struct VS_OUTPUT{
	float4 vPosition : POSITION0;
	float3 vPositionObjSpace : TEXCOORD0;
	float3 vPositionWorld : TEXCOORD1;
};

VS_OUTPUT MainVS(float3 vInPosition : POSITION0){
	VS_OUTPUT o = (VS_OUTPUT)0;
	o.vPosition = mul(float4(vInPosition,1.0f),g_mWorldViewProj);
	o.vPositionObjSpace = vInPosition;
	o.vPositionWorld = mul(float4(vInPosition,1.0f),g_mWorld);
	return o;
}

struct PS_INPUT{
	float3 vPositionObjSpace : TEXCOORD0;
	float3 vPositionWorld : TEXCOORD1;
};

float4 MainPS(PS_INPUT i):COLOR{
	float4 vResultPos = mul(float4(i.vPositionObjSpace,1.0f),g_mWorldViewProj);
	float Depth = vResultPos.z/vResultPos.w-0.000001f;
	return float4(Depth,Depth,Depth,1);
}

float4 ShadowPS(PS_INPUT i):COLOR{
	float4 vResultPosLight = mul(float4(i.vPositionObjSpace,1.0f),g_mLightWorldViewProj);
	vResultPosLight /= vResultPosLight.w;
	float Depth = vResultPosLight.z;
	float2 ScreenPos = vResultPosLight.xy*float2(0.5,-0.5)+float2(0.5,0.5);
	float4 ResultColor = float4(1,1,1,1);
	if (ScreenPos.x<1.0f&&ScreenPos.x>0.0f&&ScreenPos.y<1.0f&&ScreenPos.y>0.0f){
		float4 TexColor = tex2D(s0,ScreenPos);
		float DepthLight = TexColor.r;
		if (Depth>DepthLight){
			ResultColor = float4(1,1,1,0.2);
			float3 LightToPos = i.vPositionWorld-g_Light.vPosition;
			float d = length(LightToPos);
			LightToPos = normalize(LightToPos);
			float cosalpha = dot(LightToPos,g_Light.vDirection);
			float cosphi = cos(g_Light.Phi/2);
			float costheta = cos(g_Light.Theta/2);
			float spot = pow(saturate((cosalpha-cosphi)/(costheta-cosphi)),g_Light.Falloff);
			ResultColor.a = lerp(0.2,1.0,1.0-spot);
		}
	}
	return ResultColor;
}

float4 BlendPS(float2 vTexcoord:TEXCOORD0):COLOR{
	return tex2D(s0,vTexcoord);
}

float2 g_Offset[15] : FLOATARRAY0;
float4 g_Weight[15] : FLOATARRAY1;

float4 GaussPS(float2 vTexcoord:TEXCOORD0):COLOR{
	float4 ResultColor=0;
	for (int i=0; i<15; i++){
		ResultColor += tex2D(s0,vTexcoord+1.0f*g_Offset[i])*g_Weight[i];
	}
	return ResultColor;
}

technique DepthTech{
	pass p0{
		vertexShader = compile vs_2_0 MainVS();
		pixelShader = compile ps_2_0 MainPS();
		CullMode = CW;
	}
}

technique ShadowTech{
	pass p0{
		vertexShader = compile vs_2_0 MainVS();
		pixelShader = compile ps_3_0 ShadowPS();
	}
}

technique ShadowBlendTech{
	pass p0{
		pixelShader = compile ps_2_0 BlendPS();
		AlphaBlendEnable = True;
		SrcBlend = Zero;
		DestBlend = SrcAlpha;
	}
}

technique GaussTech{
	pass p0{
		pixelShader = compile ps_2_0 GaussPS();
	}
}